﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GodPl.Parser;
using Antlr.Runtime;

namespace GodPl
{
    public partial class Launcher : Form
    {
        private string cSharpCode;

        public Launcher()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnLaunch_Click(object sender, EventArgs e)
        {
            LaunchGame();
        }

        public void LaunchGame()
        {
            GodPlGame.StartPlaying();
            this.Close();
        }


        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textFilePath.Text = openFileDialog1.FileName;
                btnCompile.Enabled = true;
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void btnCompile_Click(object sender, EventArgs e)
        {
            try
            {
                cSharpCode = GodPlConverter.ConvertFromFile(textFilePath.Text);
            }
            catch (Antlr.Runtime.RecognitionException err)
            {
                txtOutput.Text += "\r\n\r\nLexing or parsing error:\r\n\r\n" + err.Message;
                return;
            }
            txtOutput.Text += "\r\n\r\n Parsed GodPl code and generated C# code successfully.";
            GodPlConverter.WriteToFile(textFilePath.Text + ".cs", cSharpCode);

            try
            {
                GodPlCompiler.CompileUserBriyyaUpdater(cSharpCode);
                btnLaunch.Enabled = true;
            }
            catch (ApplicationException err)
            {
                txtOutput.Text += "\r\n\r\n" + err.Message;
                return;
            }

            txtOutput.Text += "\r\n\r\n Compiled C# code successfully.";

        }
    }
}
